#pragma once

namespace byh
{
	template<class K, class V>
	class map
	{
	public:
		struct KeyOfMap
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};

		typedef typename RBTree<K, pair<const K, V>, KeyOfMap>::iterator iterator;
		typedef typename RBTree<K, pair<const K,V>, KeyOfMap>::const_iterator const_iterator;

		pair<iterator, bool> insert(const pair<K, V>& key)
		{
			return _t.insert(key);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
		}

		iterator begin()
		{
			return _t.begin();
		}

		iterator end()
		{
			return _t.end();
		}

		const_iterator begin() const
		{
			return _t.begin();
		}

		const_iterator end() const
		{
			return _t.end();
		}

	//protected:
		RBTree<K, pair<const K, V>, KeyOfMap> _t;
	};

	void Print(const map<string, int>& m)
	{
		map<string, int>::const_iterator it = m.begin();
		while (it != m.end())
		{
			//*it += 3;
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}

	void Test_map()
	{
		byh::map<string, int> m;
		m.insert({ "watermelen",2});
		m.insert({ "orange",6});
		m.insert({ "strawberry",1});
		m.insert({ "apple",2 });
		m.insert({ "watermelon",9 });
		m.insert({ "strawberry",7});

		map<string, int>::iterator it = m.begin();
		while (it != m.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}

	void Test_map2()
	{
		byh::map<string, int> m1;
		m1.insert({ "watermelen",2 });
		m1.insert({ "orange",6 });
		m1.insert({ "strawberry",1 });
		m1.insert({ "apple",2 });
		m1.insert({ "watermelon",9 });
		m1.insert({ "strawberry",7 });

		//map<string, int> copy(m);
		//Print(copy);
		//cout << copy._t.IsBalance() << endl;
		map<string, int> m2;
		m2 = m1;
		Print(m2);
	}

	void Test_map3()
	{
		vector<string> v = { "orange","apple","watermelon","strawberry","apple","orange","banana","apple" };
		byh::map<string, int> m;
		for (auto& e : v)
			m[e]++;

		for (auto& e : m)
			cout << e.first << ":" << e.second << endl;
		cout << endl;
	}
}